package com.yuxi;

/**
 * Created by yuxi on 17/1/28.
 */
public class SynchronizedTwo {
    public static void main(String[] args) {
        NotBlock notBlock = new NotBlock();
        new Thread(new MyThreadOne(notBlock)).start();
        new Thread(new MyThreadTwo(notBlock)).start();

    }

    static class MyThreadOne implements Runnable {
        NotBlock notBlock;

        public MyThreadOne(NotBlock notBlock) {
            this.notBlock = notBlock;
        }

        public void run() {
            notBlock.test1();
        }
    }

    static class MyThreadTwo implements Runnable {
        NotBlock notBlock;

        public MyThreadTwo(NotBlock notBlock) {
            this.notBlock = notBlock;
        }

        public void run() {
            notBlock.test2();
        }
    }

    static class NotBlock {

        public synchronized  void test1() {
            try {
                System.out.println(Thread.currentThread().getName() + "..." + System.currentTimeMillis());
                Thread.sleep(5000);
                System.out.println(Thread.currentThread().getName() + "..." + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public  synchronized  void test2() {
            try {
                System.out.println(Thread.currentThread().getName() + "..." + System.currentTimeMillis());
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName() + "..." + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
